<template>
	<view>
	
			<!-- 会话列表-start -->
			<view v-if="token" class="session-list">
				<block v-for="(item, index) in sessionListTop" :key="item.id">
					<session @click.native="goToSessionInfo(item.id, item.type, item.chat_id)" @longpress.native="sessionLongpress(item.id, item.top)" :item="item"></session>
				</block>
				<block v-for="(item, index) in sessionList" :key="item.id">
					<session @click.native="goToSessionInfo(item.id, item.type, item.chat_id)" @longpress.native="sessionLongpress(item.id, item.top)" :item="item"></session>
				</block>
				<view class="im-data-none" v-if="loadStatus">{{loadStatus}}</view>
			</view>

	</view>
</template>

<script>

	export default {
		data() {
			return {
			indexData: {},
				token:"",
				isShow:0,
				userInfo:{},
				list:[],
				config:{},
				titles:'聊天列表',
				keywords: '',
				maskShow: false,
				messageMenu: {
					show: false
				},
				sessionList: [],
				sessionListTop: [],
				TODOcount: 0,
				pageDataLoadBool: false,
				sessionLongpressList: [],
				sessionLongpressShow: false,
				loadStatus: '加载中...',
				commonTips: ''
			}
		},
		onLoad: function () {
			// this.site_config()
			this.ws.pageFun(this.pageDataLoad, this);
			// this.index()
		},
		onShow: function () {
			var token = uni.getStorageSync('token')
			this.token= uni.getStorageSync('token')
			if (token) {
				// console.log('sessionListTop',this.sessionListTop,'sessionList',this.sessionList)
				// console.log('sessionListTop',this.sessionListTop.length,'sessionList',this.sessionList.length)
			// this.userInform()	
			// 	this.detail()
			
			
			var that = this
			// this.ws.pageRefresh.message = true
			if(this.ws.pageRefresh.message) {
				console.log('刷新')
			// console.log('pageDataLoad',this.pageDataLoad)
				// 被其他页面通知刷新会话列表(更新列表中的好友备注等)
				this.ws.pageFun(this.pageDataLoad, this);
				this.ws.clearPageRefresh()
				this.ws.pageRefresh.message = false
				this.ws.pageRefresh.sessionInfo = false
				this.ws.messageShow = [] // 已经重载,无需执行messageShow中的方法
				return ;
			}
			if (this.ws.messageShow.length) {
				for (let m in this.ws.messageShow) {
					if (typeof this.ws.messageShow[m] == 'function') {
						this.ws.messageShow[m](that)
						this.ws.messageShow[m] = null
					}
				}
			}
			if (!this.pageDataLoadBool) {
				return ;
			}
			that.ws.checkNetwork(that)
			that.ws.pageFun(function(){
				that.ws.send({
					c: 'ImBase',
					a: 'loadTODO',
					data: {
						'method': 'get-all-count'
					}
				})
			}, that);
			}
		},
		onPullDownRefresh: function () {
			this.ws.pageFun(this.pageDataLoad(true), this);
			this.ws.onMessageCallBack.set('load_session_list', (msg) => {
				if (msg.data.data.refresh && msg.data.data.method == 'load') {
					this.$refs.uToast.show({
						title: '刷新成功~',
						type: 'success'
					})
					uni.stopPullDownRefresh()
				}
			})
		},
		methods: {
		
			pageDataLoad: function (refresh = false) {
				this.pageDataLoadBool = true
				this.ws.send({
					c: 'User',
					a: 'loadSessionList',
					data: {
						'refresh': refresh
					}
				})
				this.ws.send({
					c: 'ImBase',
					a: 'loadTODO',
					data: {
						'method': 'get-all-count'
					}
				})
			},
			showToast(title) {
				uni.showToast({
					title: title,
					icon: 'none'
				})
			},
			goToSessionInfo: function(id, type, chat_id) {
			
				var url = '/bundle/pages/session-info/session-info?id=' + id
				if (type == 'service') {
					if (chat_id != 3) {
						url = '/bundle/pages/session-info/notice-session-info?session_id=' + id
					}
				}else{
					if(!this.userInfo.is_auth){
						this.showToast('请先实名认证')
						uni.navigateTo({
							url:'/bundle/pages/my/authentication'
						})
						return
					}

					if(!Number(this.userInfo.chat_vip)&&Number(this.userInfo.gender)==1){
						this.showToast('请先购买会员')
						// setTimeout(() => {
							uni.navigateTo({
								url:'/bundle/pages/my/memberCenter?type=1'
							})
						// }, 2000)
						return
					}
				}
				uni.navigateTo({
					url: url
				})
			},
			sessionLongpress: function (id, top) {
				this.sessionLongpressShow = true
				this.sessionLongpressList = [{
					text: top ? '取消置顶':'置顶',
					id: id
				}, {
					text: '移除会话',
					color: '#f74c31',
					id: id
				}]
			},
			sessionLongpressAction: function (idx) {
				var that = this
				if (idx == 0) {
					that.ws.pageFun(function(){
						that.ws.send({
							c: 'User',
							a: 'sessionOperation',
							data: {
								id: that.sessionLongpressList[idx].id,
								action: 'session-top',
								source: 'uni-app'
							}
						})
					}, that)
				} else if (idx == 1) {
					that.ws.pageFun(function(){
						that.ws.send({
							c: 'User',
							a: 'sessionOperation',
							data: {
								action: "session-remove",
								id: that.sessionLongpressList[idx].id,
								source: 'uni-app'
							}
						});
					}, that)
				}
			},
			maskClick: function() {
				this.messageMenu.show && this.toggleMessageMenu()
			},
			toggleMessageMenu: function(e) {
				var that = this
				if (that.messageMenu.show) {
					that.maskShow = false
					that.messageMenu.show = false
					return ;
				}
				that.messageMenu = {
					show: true,
					top: (e.detail.y + 14) + 'px',
					left: (e.detail.x - 120) + 'px'
				}
				that.maskShow = true
			},
			search: function() {
				if (!this.keywords) {
					this.$refs.uToast.show({
						title: '请输入关键词~',
						type: 'error'
					})
					return ;
				}
				uni.navigateTo({
					url: '/pages/search/search?keywords=' + this.keywords,
					success: () => {
						this.keywords = ''
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
